package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Permission;
import cn.wolfcode.crud.domain.Role;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.service.IPermissionService;
import cn.wolfcode.crud.service.IRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    protected IRoleService arole;
    @Autowired
    protected IPermissionService permissionService;
    //查询操作
    @RequiresPermissions(value = {"role:list","角色列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public  String list(Model model, @ModelAttribute("qo") QuerObjcet qo){
        model.addAttribute("PageResult",arole.query(qo));
        return "role/list";
    }
    //删除操作
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id !=null){
            arole.delete(id);
        }
        return "redirect:/role/list";
    }
    //跳转页面
    @RequiresPermissions(value = {"role:input","角色编辑"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        if (id !=null){
            Role role = arole.get(id);
            model.addAttribute("role",role);
        }
        return "/role/input";
    }
    //添加和修改
    @RequiresPermissions(value = {"role:inputxxx","角色编辑xxx"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){

        if (role.getId() !=null){
            arole.update(role,ids);
        }else {
            arole.insert(role,ids);
        }

        return "redirect:/role/list";
    }


}
